package com.example.image.util;

import java.util.ArrayList;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;

public class ImageUtil {
	/**
	 * 获取图片格式列表
	 * 
	 * @return
	 */
	public static ArrayList<String> getImageTypes() {
		ArrayList<String> list = new ArrayList<String>();
		list.add("JPEG");
		list.add("JPG");
		list.add("PNG");
		list.add("GIF");
		list.add("BMP");
		return list;
	}

	/**
	 * 根据图片路径返回图片的drawable缩略图
	 * 
	 * @param path
	 *            图片路径
	 * @param width
	 *            缩略图的宽
	 * @return
	 */
	public static Drawable getDrawable(String path, int width) {
		Bitmap bitmap = getBitmap(path, width);
		return new BitmapDrawable(bitmap);
	}

	/**
	 * 根据图片路径返回图片的bitmap缩略图
	 * 
	 * @param path
	 *            图片路径
	 * @param width
	 *            缩略图的宽
	 * @return
	 */
	public static Bitmap getBitmap(String path, int width) {
		Bitmap bitmap = BitmapFactory.decodeFile(path);
		int height = bitmap.getHeight() * width / bitmap.getWidth();
		return ThumbnailUtils.extractThumbnail(bitmap, width, height,
				ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
	}

	/**
	 * 旋转图片
	 * 
	 * @param currentBitmap
	 * @return
	 */
	public static Bitmap turn(Bitmap currentBitmap, float degrees) {
		Matrix matrix = new Matrix();
		matrix.postRotate(degrees);
		return Bitmap.createBitmap(currentBitmap, 0, 0,
				currentBitmap.getWidth(), currentBitmap.getHeight(), matrix,
				true);
	}

	/**
	 * 缩放图片
	 * 
	 * @param currentBitmap
	 * @param sx
	 *            缩放的比例
	 * @return
	 */
	public static Bitmap zoom(Bitmap currentBitmap, float scale) {
		Matrix matrix = new Matrix();
		matrix.postScale(scale, scale);
		return Bitmap.createBitmap(currentBitmap, 0, 0,
				currentBitmap.getWidth(), currentBitmap.getHeight(), matrix,
				true);
	}
}
